package org.nvframe.component.property;

import java.util.ArrayList;
import java.util.List;

import org.nvframe.component.property.operation.*;

public class TransientPropertyFloat implements TransientProperty {
	
	private float persistentVal;
	private float cachedTransientVal;
	
	private List<Operation> operations;

	public TransientPropertyFloat(float persistentVal) {
		this.persistentVal = persistentVal;
		
		operations = new ArrayList<Operation> ();
		cachedTransientVal = 0;
	}

	private float reCalculateCachedValue() {
		float val = persistentVal;
		
		for(Operation operation : operations)
			val = operation.getResult(val);
		
		cachedTransientVal = val;
		
		return val;
	}
	
	public float getPersistentVal() {
		return persistentVal;
	}
	
	public float getValue() {
		return cachedTransientVal;
	}
	
	public void add(float addVal) {
		operations.add(new Addition(addVal));	
		reCalculateCachedValue();
	}
	
	public void substract(float substractVal) {
		operations.add(new Substraction(substractVal));	
		reCalculateCachedValue();
	}
	
	public void multiply(float multiplyVal) {
		operations.add(new Multiplication(multiplyVal));	
		reCalculateCachedValue();
	}
	
	public void divide(float divideVal) {
		operations.add(new Division(divideVal));	
		reCalculateCachedValue();
	}
	
	public void percentage(float percentage) {
		operations.add(new Percentage(percentage));	
		reCalculateCachedValue();
	}
	
}
